<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="../../layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../layui/css/formSelects-v4.css" media="all">
    <link rel="shortcut icon" href="../../statics/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../../statics/css/font.css">
    <link rel="stylesheet" href="../../statics/css/xadmin.css">
    <script src="../../statics/js/jquery.min.js"></script>
    <script type="text/javascript" src="../../statics/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../statics/js/xadmin.js"></script>
</head>

<body>
<div class="x-body">
    <form class="layui-form" lay-filter="userModifyForm">
        <div class="layui-form-item" style="display: none">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>id
            </label>
            <div class="layui-input-inline">
                <input type="text" id="id" name="id" required
                       autocomplete="off" value="" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>用户名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="username" name="username" required lay-verify="required"
                       autocomplete="off" value="" disabled class="layui-input layui-disabled" readonly="readonly" >
            </div>
        </div>


        <div class="layui-form-item">
            <label for="phone" class="layui-form-label">
                <span class="x-red">*</span>手机
            </label>
            <div class="layui-input-inline">
                <input type="text" value="" id="phone" name="phone" required lay-verify="phone"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="email" class="layui-form-label">
                <span class="x-red">*</span>邮箱
            </label>
            <div class="layui-input-inline">
                <input type="text" value="" id="email" name="email" required lay-verify="email"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="userNo" class="layui-form-label">
                <span class="x-red">*</span>学号
            </label>
            <div class="layui-input-inline">
                <input type="text" value="" id="userNo" name="userNo" required lay-verify="number"
                       autocomplete="off" disabled class="layui-input layui-disabled">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"> <span class="x-red">*</span>用户角色</label>
            <div class="layui-input-inline" style="width: 300px">
                <select name="roleIds" id="roles_add" xm-select="roles2" xm-select-direction="down">
                </select>
            </div>
        </div>


        <div class="layui-form-item">
            <label class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                提交修改
            </button>
        </div>
    </form>
</div>
<script>

    layui.config({
        base: '../../layui/lay/modules/'
    }).extend({
        formSelects: 'formSelects-v4'
    });

    layui.use(['form', 'layer','formSelects'], function () {
        var formSelects = layui.formSelects;
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer;

        //自定义验证规则
        form.verify({
            number:[/^\d{6,}$/,'学号格式为全数字或学号位数有误']
        });

        //监听提交
        form.on('submit(add)', function (data) {
            console.log(data);
            debugger;
            //发异步，把数据提交给php
            layer.msg("增加成功", {icon: 6,time:1000}, function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
            });
            $.ajax({
                url:"/user/commitUserModifyInfo",
                data:{
                    id:data.field.id,
                    username:data.field.username,
                    phone:data.field.phone,
                    email:data.field.email,
                    userNo:data.field.userNo,
                    roleIds:layui.formSelects.value('roles2','valStr')
                }
            });
            return false;
        });
    });

    $.post("/role/getAllRole",function (result) {
        if(result.code===0){
            for(var i=0;i<result.data.length;i++){
                var opt = $("<option></option>").appendTo("#roles_add");
                opt.text(result.data[i].roleName).val(result.data[i].id);
            }
            //等待父页面将id付给子页面的id标签value中
            setTimeout(function () {
                var id = $("#id").val();
                $.ajax({
                    url:"/role/getAllRoleByUserId",
                    data:{
                        userId:id
                    },
                    success:function (result) {
                        var roleArray = new Array();
                        for(var i=0;i<result.data.length;i++){
                            roleArray.push(result.data[i].id);
                        }
                        layui.formSelects.value('roles2',roleArray);
                    }
                })
            },200);
            layui.formSelects.render();
        }
    });
</script>
</body>

</html>